package org.csu.mypetstore.persistence;

import java.sql.*;

//工具类
public class DBUtil {
    //private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";//mySql 5+
    private static final String DRIVER_CLASS = "com.mysql.cj.jdbc.Driver";//mySql 8+
    private static final String URL = "jdbc:mysql://127.0.0.1:3306/mypetstore?useSSL=false";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "yr011005";

    public static Connection getConnection() throws Exception{
        Class.forName(DRIVER_CLASS);
        Connection connection = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        return connection;
    }

    //关闭Connection
    public static void closeConnection(Connection connection) throws Exception{
        if(connection != null){
            connection.close();
        }
    }
    //关闭Statement
    public static void closeStatement(Statement statement) throws Exception{
        if(statement != null){
            statement.close();
        }
    }
    //关闭执行对象PreparedStatement
    public static void closePreparedStatement(PreparedStatement preparedStatement) throws Exception{
        if(preparedStatement != null){
            preparedStatement.close();
        }
    }
    //关闭结果集ResultSet
    public static void closeResultSet(ResultSet resultSet) throws Exception{
        if(resultSet != null){
            resultSet.close();
        }
    }

    /*测试
    public static void main(String[] args) throws Exception{
        Connection conn = DBUtil.getConnection();
        System.out.println(conn);
    }
    */
}
